<template>
  <div class="app-container">
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="全部" name=""></el-tab-pane>
      <el-tab-pane
          :key="item.dictValue"
          v-for="item in equipmentOptions"
          :label="item.dictLabel"
          :name="item.dictValue"
      >
      </el-tab-pane>
    </el-tabs>
    <uc-table url="admin/equipment/getMeasuringEquipmentPage" name="检测设备" ref="measuringEquTable" pageable table-border
              :search-form="searchForm">
      <template slot="searchForm">
        <el-form size="mini" :inline="true" :model="searchForm" class="demo-form-inline filter-container">
          <!--搜索输入框-->
          <el-form-item>
            <el-input type="text" v-model="searchForm.name" placeholder="请输入设备名称" clearable
                      @clear="clearInput()"></el-input>
          </el-form-item>
          <el-form-item v-if="enterPriseHidden">
            <el-select v-model="searchForm.enterpriseId" placeholder="请选择主体" clearable @clear="clearInput1">
              <el-option v-for="(item,index) in enterpriseDominantList" :key="index" :value="item.ucId"
                         :label="item.name"></el-option>
            </el-select>
          </el-form-item>
          <!--搜索按钮--新增按钮-->
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" class="filter-item" @click="handleSearch()">查询</el-button>
            <el-button type="success" icon="el-icon-plus" class="filter-item" @click="handleAdd()">新 增</el-button>
          </el-form-item>
        </el-form>
      </template>
      <template slot="columns">
        <el-table-column prop="name" label="名称" align="left"></el-table-column>
        <el-table-column prop="enterpriseName" label="企业名称" align="left" show-overflow-tooltip></el-table-column>
        <!--                <el-table-column prop="node" label="产品ID" align="left" show-overflow-tooltip></el-table-column>-->
        <el-table-column prop="imei" label="imei" align="left" show-overflow-tooltip></el-table-column>
        <!--                <el-table-column prop="type" label="产品类型" align="left" show-overflow-tooltip></el-table-column>-->
        <!--                <el-table-column prop="devCollectName" label="采集设备" align="left" show-overflow-tooltip></el-table-column>-->
        <el-table-column prop="type" label="设备类型" align="left" :formatter="statusFormat" show-overflow-tooltip/>
        <el-table-column prop="address" label="操作" width="240" align="left" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" icon="el-icon-edit" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" icon="el-icon-info" type="success" @click="handleInfo(scope.row)">详情</el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete" :loading="deleteLoading"
                       @click="handleDelete(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </template>
    </uc-table>
  </div>
</template>

<script>
import UcTable from "@/components/UcTable/index.vue";
import {deleteInMeasuringEquipment} from "@/api/equipment";
import {getInEnterpriseList} from "@/api/enterprise"
import {getInEnterpriseDominantList} from "@/api/enterpriseDominant";
import {mapGetters} from "vuex";

export default {
  name: "equipmentList",
  components: {UcTable},
  data() {
    return {
      searchForm: {
        deleted: false,
        type: null
      },
      enterpriseDominantList: [],
      equipmentOptions: [],
      deleteLoading: false,
      activeName: ""
    }
  },
  created() {
    this.getDicts("equipment_jc_type").then(response => {
      this.equipmentOptions = response.data.data;
    });
    this.getInEnterpriseDominantList()
  },
  computed: {
    enterPriseHidden() {
      let roleCode = this.$store.getters.roleCode;
      return roleCode == "ADMIN" || roleCode == "XT-ADMIN" || roleCode == "ZD-ADMIN" || roleCode == "DD-ADMIN" || roleCode == "JW-ADMIN" || roleCode == "WB-ADMIN"
    },
    ...mapGetters(["fireAlarm"]),
  },
  methods: {
    handleClick(val) {
      if (val.name == "") {
        this.searchForm.type = null
      } else {
        this.searchForm.type = val.name
      }
      this.handleSearch()
    },
    statusFormat(row) {
      return this.selectDictLabel(this.equipmentOptions, row.type);
    },
    handleAdd() {
      this.$router.push("/equipment/measuringEquEdit/0")
    },
    handleEdit(val) {
      this.$router.push("/equipment/measuringEquEdit/" + val.ucId)
    },
    handleInfo(val) {
      this.$router.push("/equipment/measuringEquInfo?ucId=" + val.ucId + "&type=" + val.type)
    },
    handleDelete(val) {
      this.$confirm('您确定要执行删除【' + val.name + '】这个设备吗？', '提醒信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteLoading = true
        deleteInMeasuringEquipment(val).then(res => {
          this.deleteLoading = false
          this.$notify.success({
            title: '成功',
            message: "删除检测设备成功"
          })
          this.handleSearch()
        }).catch(res => {
          this.deleteLoading = false
          this.$notify.error({
            title: '失败',
            message: res.msg
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    handleSearch() {
      this.$refs.measuringEquTable.getTableData(1)
    },
    clearInput1() {
      //清楚企业下拉框
      this.searchForm.enterpriseId = null
      this.handleSearch()
    },
    getInEnterpriseDominantList() {
      getInEnterpriseDominantList({deleted: false}).then(res => {
        this.enterpriseDominantList = res.data
      }).catch(error => {

      })
    }
  },
  watch: {
    fireAlarm: function (a, b) {
      if (a !== b && a) {
        console.log(a)
        this.$refs.audio.currentTime = 0;
        this.$refs.audio.play();
        this.$fireAlarm.show(a).then(res => {
          switch (res) {
            case "enterInfo":
              this.$router.push("/fireAlarm/fireInfoInfo/" + a.ucId)
              break;
            default:
              break;
          }
        })
      }
    }
  },
}
</script>

<style scoped>

</style>
